#!/bin/bash
##根据环境区分部署的IP数组值

echo "环境参数:" $env ",分支名称:" $CI_BUILD_REF_NAME

if [[ $env == 'dev' && $CI_BUILD_REF_NAME == 'develop' ]]; then
   argv=(10.129.37.82)
elif [[ $env == 'uat' ]] && [[ $CI_BUILD_REF_NAME == 'uat' ]]; then
   argv=(10.129.37.104 10.129.37.105)
elif [[ $env == 'prod' ]] && [[ $CI_BUILD_REF_NAME == 'master' ]]; then
   argv=(10.129.2.8 10.129.2.9)
else
   echo 'Please enter env parameter 【dev|uat|prod】'
   exit 1
fi

#遍历IP并部署：部署先将gitlab打的包放到机器的/tmp目录下，然后登陆目标机器将包放到部署目录，并删除/tmp下内容
for i in "${argv[@]}";do
    scp -r -o stricthostkeychecking=no publish/* cifiadmin@$i:/tmp/ && \
    ssh -o PasswordAuthentication=no -o StrictHostKeyChecking=no cifiadmin@$i "
    cd /home/cifiadmin/web ;
    rm -rf ./* ;
    cp -r /tmp/pcdist/* ./ ;
    ls -al ;
    rm -rf /tmp/pcdist/* ;
    exit 0 ;" || exit 1
done
echo 'deploy successful!'